<?php
class Str extends FluentExt{
	function __construct($str){
		$str = is_string($str) ? $str : ""; 
		$this->c = $str; 
	}
	
	static function fluent($str){ return new Str($str); }
		
	function str(){ return $this->c; }
	
	function format(){
		$params 	= func_get_args();
		$call_arr 	= array_merge(array($this->c), $params);
		$this->c 	= call_user_func_array('sprintf', $call_arr);
		return $this;
	}
	
	function unserial(){
		$result = unserialize($this->c);
		if(is_array($result)) 	return new Arr($result);
		else					$this->c = (string)$result;
		return $this;
	}
	
	function repeat($times){ $this->c = str_repeat($this->c, $times); return $this; }
	
	function getChr($pos){ return $this->c{$pos}; }
	
	function first(){ return $this->getChr(0); }
	
	function last(){ return $this->getChr(strlen($this->c) - 1); }
	
	function add($str){ $this->c .= $str; return $this; }
	
	private function match_common($pattern, $flag, $offset, $pos = false){
		preg_match_all($pattern, $this->c, $matches, $flag, $offset);
		if($flag == PREG_OFFSET_CAPTURE) 		foreach($matches[0] as $match) 	$rarr[] = $match[1];
		else if($flag == PREG_PATTERN_ORDER) 	foreach($matches[0] as $match) 	$rarr[] = $match;
		else									foreach($matches as $match) 	$rarr[] = $match[$pos];
		return new Arr($rarr);
	}
	
	function match($pattern, $offset = 0){ 
		return $this->match_common($pattern, PREG_PATTERN_ORDER, $offset); 
	}
	
	function matchPos($pattern, $offset = 0){ 
		return $this->match_common($pattern, PREG_OFFSET_CAPTURE, $offset); 
	}
	
	function matchArb($pattern, $flag, $offset, $pos){ 
		return $this->match_common($pattern, $flag, $offset, $pos); 
	}
	
	function matchFirstPos($pattern){ return preg_match($pattern, $this->c); }
	
	function matchReplace($pattern, $replacement, $limit = -1){
		$this->c 		= preg_replace($pattern, $replacement, $this->c, $limit, $count);
		$this->count 	= $count;
		return $this;
	}
	
	function matchCount($pattern, $offset = 0){
		preg_match($pattern, $this->c, $matches, PREG_OFFSET_CAPTURE, $offset);
		return count($matches);
	}
	
	function replace($search, $replace){
		$this->c = str_replace($search, $replace, $this->c);
		return $this;
	}
	
	function out(){ echo $this->c; }
	
	function pos($str, $offs = 0){ return strpos($this->c, $str, $offs); }
	
	function lastPos($str, $offs = 0){ return strrpos($this->c, $str, $offs); }
	
	function slice($start, $length){ $this->c = substr($this->c, $start, $length); return $this; }
	
	function tail($str){ $this->c = strrchr($this->c, $str); return $this; }
	
	function addSlashes(){ $this->c = addslashes($this->c); return $this; }
	
	function stripSlashes(){ $this->c = stripslashes($this->c); return $this; }
	
	function split($sep = ' '){ return new Arr(explode($sep, $this->c)); }
	
	function includes($str, $offs = 0){ return $this->pos($str, $offs) === false ? false : true; }
	
	function length(){ return strlen($this->c); }
	
	function reverse(){ $this->c = strrev($this->c); return $this; }
		
	function capitalize(){ $this->c = ucfirst($this->c); return $this; }
	
	function trim($chars = ""){ $this->c = trim($this->c, $chars); return $this; }
	
	function downCase(){ $this->c = strtolower($this->c); return $this; }
	
	function upperCase(){ $this->c = strtoupper($this->c); return $this; }
	
}
?>